
(function(){
	//查看支付成功没有
	let tradeNo = json.getUrlData('trade_no');
	json.post(URL + '/api_payquery', {
		userId: TOKEN,
		tradeNo: tradeNo
	}, res => {
		console.log(res);
		if(res.code != 0){
			return;
		}
		orderList();
	})
	
	console.log(TOKEN);
	orderList();
	//渲染订单
	// location.reload();
	function orderList(){
		
		json.post(URL + '/api_order', {
		userId: TOKEN,
		status: 'vieworder',
		},even =>{
			console.log(even);
			if(even.code != 0){
				console.log(even);
				return;
			};
			let order = document.querySelector(".order");
			let orderinfoout = document.querySelector(".order-info-out");
			let status = document.querySelector(".status")
			// console.log(orderinfoout)
			let str = `
				<div class="order-top flex-row">
					<div>宝贝</div>
					<div>单价</div>
					<div>数量</div>
					<div>商品操作</div>
					<div>实付款</div>
					<div>交易状态</div>
				</div>
			`;
			
			
			
			even.data.forEach(even =>{
				console.log("1")
				let div = "";
				
				even.goods.forEach(item =>{
					// console.log("22")
					//  let div = document.createElement("div");
					  div += `
					<div class="order-info flex-row" >
						<div class="good-img flex-row" >
							<img src="${item.goods_thumb}"/>
							
							<div>
								<p>${item.goods_name}</p>
								<p>${item.goods_desc}</p>
							</div>
						</div>
						<div class="price">${item.price}￥</div>
						<div class="order-num">${item.goods_number}</div>
						<div class="pppp">申请售后</div>
					</div>
					`;
					// orderinfoout.appendChild(div)
					
				});
				
				str +=`
				  <div class="order-list">
				  	<div class="order-list-top flex-row align-center">
				  		<div class="order-id">订单号：<span>${even.trade_no}</span></div>
				  		<div class="brand-name">${even.goods[0].brand_name}</div>
				  		<div class="del" trade_no = ${even.trade_no} ><img src="img/del.png"/></div>
				  	</div>
				  	
				  	<div class="order-list-info flex-row">
				  		    <div class="order-info-out">
								${div}
					        </div>
							
							<div class="pricesum">999￥</div>
							<div class="order-status">${even.status}</div>
							
						</div>
					
					
					</div>
						
				`
			});
			
			order.innerHTML = str;
			// location.href = "myproduct.html";
			
			
			
			let pricesum = document.querySelectorAll(".pricesum");
			// console.log(pricesum)
			
			
			even.data.forEach((even,index) =>{
				let sum = 0;
				even.goods.forEach(item =>{
					sum+= item.price * item.goods_number;
					
				});
			
				pricesum[index].innerHTML = sum + "￥";
			});
			
			
			
			let delBnt = document.querySelectorAll(".del");
			 // console.log(delBnt)
			//删除订单
			delBnt.forEach(even =>{
				even.onclick = function(){
					// console.log("111")
					// console.log(this.getAttribute("trade_no"))
					
					json.post(URL + "/api_order",{
						userId: TOKEN,
						status:  "delorder",
						tradeNo: this.getAttribute("trade_no"),
					},even =>{
						console.log(even)
						if(even.code != 0){
							return;
						};
						alert("删除订单成功")
						orderList()
						
					})
					
				};
				
			});
			
			
			
		})
	}
	
	

	
	
})();